/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package test.service.subrel;

import java.util.Date;

import test.base.BaseServiceTester;

import com.wilmar.process.core.services.api.ISystemSubProcessInstanceRelationService;
import com.wilmar.process.entity.SubProcessInstanceRelation;
import com.wilmar.process.exception.ObjectNotFoundException;

/**
 * @author makubex Email:makubexnet@gmail.com
 * @version 1.0.0 Jun 18, 2010
 * @since 1.0.0
 */
public class SubProcessRelationServiceTestCase extends BaseServiceTester {
	protected ISystemSubProcessInstanceRelationService process_subProcessInstanceRelationService;

	public void testAllService() throws ObjectNotFoundException {
		SubProcessInstanceRelation relation = new SubProcessInstanceRelation();
		relation.setCreateTime(new Date());
		relation.setParentExid("main_process_001");
		relation.setSubExid("sub_process_001");
		process_subProcessInstanceRelationService.saveRelation(relation);
		System.out.println(process_subProcessInstanceRelationService
				.getDetail(relation.getId()));
		assertEquals(process_subProcessInstanceRelationService
				.findSubInstanceExids("main_process_001").size(), 1);
		SubProcessInstanceRelation sub_relation = new SubProcessInstanceRelation();
		sub_relation.setCreateTime(new Date());
		sub_relation.setParentExid("sub_process_001");
		sub_relation.setSubExid("sub_process_002");
		SubProcessInstanceRelation sub_relation2 = new SubProcessInstanceRelation();
		sub_relation2.setCreateTime(new Date());
		sub_relation2.setParentExid("main_process_001");
		sub_relation2.setSubExid("sub_process_003");
		process_subProcessInstanceRelationService.saveRelation(sub_relation);
		process_subProcessInstanceRelationService.saveRelation(sub_relation2);
		assertEquals(process_subProcessInstanceRelationService
				.findSubInstanceExids("main_process_001").size(), 2);
		assertEquals(process_subProcessInstanceRelationService
				.findSubInstanceExids("sub_process_001").size(), 1);
		assertEquals(process_subProcessInstanceRelationService
				.findAllSubInstanceExids("main_process_001").size(), 3);
		assertEquals(process_subProcessInstanceRelationService
				.findRootInstanceExid("sub_process_002"), "main_process_001");
		assertEquals(process_subProcessInstanceRelationService
				.findParentInstanceExid("sub_process_002"), "sub_process_001");
		assertEquals(process_subProcessInstanceRelationService
				.findParentInstanceExid("sub_process_001"), "main_process_001");
		assertEquals(process_subProcessInstanceRelationService
				.findParentInstanceExid("sub_process_003"), "main_process_001");
	}

	public void testRemove() {
		process_subProcessInstanceRelationService
				.removeAllRelation("main_process_001");
	}
	
	public void testFind() {
		int count = 10;
		try {
			count = process_subProcessInstanceRelationService
					.findAllSubInstanceExids("main_process_001").size();
		} catch (ObjectNotFoundException ex) {
			count = 0;
		}
		assertEquals(count, 0);
	}
}
